package com.neusoft;

import com.neusoft.model.Student;

import java.io.*;
import java.util.*;

/**
 * @Author: duan
 * @Date: Create in 14:19 2020/10/15
 * 1、标准答案在哪里
 * 2、需要查阅的答案在哪里
 */
public class YueJuanDemo {
    private String daanPath = "D:\\学生答案\\标准答案";
    private String xueShengDaan = "D:\\学生答案\\5班";

//    private List<String> xuanzeti = new ArrayList<>();
//    private List<String> tiankongti = new ArrayList<>();
    private Map<String, List<String>> map;//保存标准答案

    public static void main(String[] args) {
        YueJuanDemo demo = new YueJuanDemo();
        demo.readBzda(demo.daanPath, new String[]{"选择题", "填空题"});
        List<Student> list = demo.readXsda(demo.xueShengDaan, new String[]{"选择题", "填空题"});
        //遍历学生的答案与标准答案
        for (Student item : list) {
            //比较选择题
            List<String> xztList = item.getXztList();
            List<String> xztBzdaList = demo.map.get("选择题");
            item.setXztScore(demo.compareScore(xztList, xztBzdaList) * 2);
            List<String> tktList = item.getTktList();
            List<String> tktBzdaList = demo.map.get("填空题");
            item.setTktScore(demo.compareScore(tktList, tktBzdaList) * 2);
            System.out.println(item.getFolder() + "：选择题，" + item.getXztScore() + ", 填空题，" + item.getTktScore());
         }
    }

    /**
     * 比较学生答案list与标准答案bzdaList
     * @param list
     * @param bzdaList
     * @return
     */
    public int compareScore(List<String> list, List<String> bzdaList) {
        int count = 0;//正确答案个数
        for (int i = 0; i < bzdaList.size(); i++) {
//            System.out.println("bzdaList.get(i)：" + bzdaList.get(i));
//            System.out.println("list.get(i)：" + list.get(i));
            if (bzdaList.get(i).equalsIgnoreCase(deleteSpace(list.get(i)))) {
                count++;
            }
        }

        return count;
    }

    /**
     * 删除content值中的所有空格
     * @param content
     * @return
     */
    public String deleteSpace(String content) {
        content = content.replace(".", ",");
        content = content.replace("，", ",");
        content = content.replace("-", ",");
        content = content.replace("、", ",");

        return content.replace(" ", "");
    }

    public List<Student> readXsda(String folder, final String[] filenames) {
        File folderFile = new File(folder);
//        System.out.println(folderFile.isDirectory());
        File[] files = folderFile.listFiles();
        List<Student> list = new ArrayList<>();
        for (File item : files) {
            Student bean = new Student();
            String name = item.getName();
//            System.out.println("学生信息：" + name);
            File[] fs = item.listFiles(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return isInArray(filenames, name);
                }
            });
            bean.setFolder(name).setXztPath(fs[1].getPath()).setTktPath(fs[0].getPath());
            //链式写法
            bean.setTktList(readFile(bean.getTktPath()))
                .setXztList(readFile(bean.getXztPath()));
//            System.out.println(bean);
//            System.out.println(Arrays.toString(fs));
            list.add(bean);
        }

        return list;
    }

    /**
     * 判断数组array是否存在str字符串
     * @param array
     * @param str
     * @return
     */
    public boolean isInArray(String[] array, String str) {
        for (String item : array) {
            if (str != null && str.indexOf(item) != -1) {
                return true;
            }
        }

        return false;
    }

    /**
     * 读取标准答案
     * @param folder
     * @param filenames
     */
    public void readBzda(String folder, String[] filenames) {
        map = new HashMap<>();
        for (String item : filenames) {
            //文件路径
            String path = folder + File.separator + item + ".txt";
            List<String> list = readFile(path);
//            System.out.println(list);
            map.put(item, list);
        }
    }

    /**
     * 读取path路径对应的文件，并返回List
     * @param path
     * @return
     */
    public List<String> readFile(String path) {
        FileReader fr = null;
        BufferedReader br = null;
        List<String> list = new ArrayList<>();//保存答案的集合
        try {
            fr = new FileReader(path);
            br = new BufferedReader(fr);
            String line;
            while ((line = br.readLine()) != null) {
                list.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return list;
    }
}
